<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="name">
        <el-input v-model="listQuery.name" size="small" placeholder="医生姓名" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="isinterior">
        <el-select v-model="listQuery.isinterior" size="small" placeholder="是否内部用户" clearable style="width: 140px" @change="handleFilter">
          <el-option v-for="item in isinteriorOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="isVip">
        <el-select v-model="listQuery.isVip" size="small" placeholder="是否VIP" clearable style="width: 110px" @change="handleFilter">
          <el-option v-for="item in isVipOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="attention">
        <el-select v-model="listQuery.attention" size="small" placeholder="是否关注" clearable style="width: 110px" @change="handleFilter">
          <el-option v-for="item in attentionOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="tel">
        <el-input v-model="listQuery.tel" size="small" placeholder="手机号码" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="email">
        <el-input v-model="listQuery.email" size="small" placeholder="邮箱" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="mruserId">
        <el-input v-model="listQuery.mruserId" size="small" placeholder="MrUserId" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="veevaid">
        <el-input v-model="listQuery.veevaid" size="small" type="number" placeholder="VeevaId" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="veevacode">
        <el-input v-model="listQuery.veevacode" size="small" placeholder="VeevaCode" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="type">
        <el-select
          v-model="listQuery.type"
          size="small"
          placeholder="请选择用户类型"
          filterable
          clearable
          @change="handleFilter"
        >
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item prop="createdtime">
        <el-date-picker
          v-model="listQuery.createdtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="注册时间"
          size="small"
          style="width: 200px;"
          @change="handleFilter"
        />
      </el-form-item>
      <el-form-item prop="attentiondate">
        <el-date-picker
          v-model="listQuery.attentiondate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="关注时间"
          size="small"
          style="width: 200px;"
          @change="handleFilter"
        />
      </el-form-item> -->
      <el-form-item prop="provinceCode">
        <el-select
          v-model="listQuery.provinceCode"
          size="small"
          placeholder="请选择省份"
          filterable
          clearable
          @change="changeProvince"
        >
          <el-option
            v-for="item in provinceOptions"
            :key="item.id"
            :label="item.province_name"
            :value="item.province_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="cityCode">
        <el-select
          v-model="listQuery.cityCode"
          size="small"
          placeholder="请选择城市"
          filterable
          clearable
          @change="changeCity"
        >
          <el-option
            v-for="item in cityOptions"
            :key="item.id"
            :label="item.city_name"
            :value="item.city_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="hospid">
        <el-select v-model="listQuery.hospid" size="small" placeholder="医院名称" filterable clearable @change="handleFilter">
          <el-option v-for="item in hospitalOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="deparrment">
        <el-select v-model="listQuery.deparrment" size="small" placeholder="科室名称" filterable clearable @change="handleFilter">
          <el-option v-for="item in deparpmentOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="openid">
        <el-input v-model="listQuery.openid" size="small" placeholder="OpenId" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="cluster">
        <el-select v-model="listQuery.cluster" size="small" placeholder="所属群组" clearable @change="handleFilter">
          <el-option v-for="item in clusterOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column type="expand">
        <template slot-scope="scope">
          <div class="table-expand-row">VeevaId：
            <span
              v-for="(item, index) in scope.row.veevaList"
              :key="index"
            >{{ item.veeva_id }}<el-divider v-if="index !== scope.row.veevaList.length - 1" direction="vertical" /></span>
          </div>
          <div class="table-expand-row">用户类型：{{ scope.row.typename }}</div>
          <div class="table-expand-row">
            内部用户：
            <i v-if="scope.row.isinterior === 1" class="el-icon-check text-success" />
            <i v-else class="el-icon-close text-warning" />
          </div>
          <div class="table-expand-row">
            Global：
            <i v-if="scope.row.isGlobal === 1" class="el-icon-check text-success" />
            <i v-else class="el-icon-close text-warning" />
          </div>
          <div class="table-expand-row">
            VIP：
            <i v-if="scope.row.level === 'vip'" class="el-icon-check text-success" />
            <i v-else class="el-icon-close text-warning" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="140">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="电话号码" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.tel }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column label="MrUserId" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.mrUserId }}
        </template>
      </el-table-column>
      <el-table-column label="VeevaCode">
        <template slot-scope="scope">
          <p
            v-for="(item, index) in scope.row.veevaList"
            :key="index"
          >{{ item.veeva_code }}</p>
        </template>
      </el-table-column>
      <el-table-column label="医院名称">
        <template slot-scope="scope">
          {{ scope.row.hospname }}
        </template>
      </el-table-column>
      <el-table-column label="科室名称" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.adfname }}
        </template>
      </el-table-column>
      <el-table-column label="所属群组" width="160" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="!_.isNil(scope.row.clusterName)"
            effect="plain"
            size="mini"
          >{{ scope.row.clusterName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="当前注册信息来源" width="140" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="!_.isEmpty(scope.row.sourcename)"
            effect="plain"
            size="mini"
          >{{ scope.row.sourcename }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建于" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.createdtime | moment('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            @click="toUserShow(scope.row.id)"
          >详情</el-button>
          <el-button
            size="mini"
            @click="toUserEdit(scope.row.id)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getUsersList
} from '@/api/users'
import {
  getProvince,
  getCities,
  getDepartments,
  getHospitalsByCity,
  getUserType,
  getCluster
} from '@/api/baseData'
import Pagination from '@/components/Pagination'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'UsersList',
  components: {
    Pagination
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        name: formatQuery(this.$route.query.name, false),
        isinterior: formatQuery(this.$route.query.isinterior, true),
        isVip: formatQuery(this.$route.query.isVip, true),
        tel: formatQuery(this.$route.query.tel, false),
        email: formatQuery(this.$route.query.email, false),
        mruserId: formatQuery(this.$route.query.mruserId, false),
        veevaid: formatQuery(this.$route.query.veevaid, false),
        veevacode: formatQuery(this.$route.query.veevacode, false),
        type: formatQuery(this.$route.query.type, true),
        provinceCode: formatQuery(this.$route.query.provinceCode, false),
        cityCode: formatQuery(this.$route.query.cityCode, false),
        hospid: formatQuery(this.$route.query.hospid, true),
        deparrment: formatQuery(this.$route.query.deparrment, true),
        openid: formatQuery(this.$route.query.openid, false),
        cluster: formatQuery(this.$route.query.cluster, true),
        attention: formatQuery(this.$route.query.attention, true),
        page: 1,
        size: 30
      },
      provinceOptions: [],
      cityOptions: [],
      hospitalOptions: [],
      deparpmentOptions: [],
      typeOptions: [],
      attentionOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      isinteriorOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      isVipOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      emailStateOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      veevaidStateOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      coverStateOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      clusterOptions: []
    }
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchProvince()
    // this.fetchHospitals()
    this.fetchDepartments()
    this.fetchCluster()
    this.fetchUserType()
    this.fetchData()

    const provinceCode = this.$route.query.provinceCode
    const cityCode = this.$route.query.cityCode
    if (!this._.isEmpty(provinceCode)) {
      this.fetchCities(provinceCode)
    }
    if (!this._.isEmpty(provinceCode) && !this._.isEmpty(cityCode)) {
      this.fetchHospitals()
    }
  },
  methods: {
    fetchProvince() {
      getProvince().then(response => {
        // console.log(response)
        this.provinceOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchCities(code) {
      getCities({ provinceCode: code }).then(response => {
        // console.log(response)
        this.cityOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchHospitals() {
      getHospitalsByCity({
        provinceCode: this.listQuery.provinceCode,
        cityCode: this.listQuery.cityCode
      }).then(response => {
        // console.log(response)
        this.hospitalOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchDepartments() {
      getDepartments().then(response => {
        // console.log(response)
        this.deparpmentOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchCluster() {
      getCluster().then(response => {
        // console.log(response)
        this.clusterOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchUserType() {
      getUserType().then(response => {
        // console.log(response)
        this.typeOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getUsersList(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    changeProvince(province) {
      this.listQuery.cityCode = ''
      this.cityOptions = []
      this.listQuery.hospid = ''
      this.hospitalOptions = []
      if (!this._.isEmpty(province)) {
        this.fetchCities(province)
      }
    },
    changeCity(city) {
      this.listQuery.hospid = ''
      this.hospitalOptions = []
      if (!this._.isEmpty(city)) {
        this.fetchHospitals()
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      // this.cityOptions = []
      // this.hospitalOptions = []
      const provinceCode = this.listQuery.provinceCode
      const cityCode = this.listQuery.cityCode
      if (!this._.isEmpty(provinceCode)) {
        this.fetchCities(provinceCode)
      } else {
        this.cityOptions = []
      }
      if (!this._.isEmpty(provinceCode) && !this._.isEmpty(cityCode)) {
        this.fetchHospitals()
      } else {
        this.hospitalOptions = []
      }
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    toUserShow(id) {
      this.$router.push(`/users/list/${id}`)
    },
    toUserEdit(id) {
      this.$router.push(`/users/edit/${id}`)
    }
  }
}
</script>
